//
//  FirstViewController.m
//  dictionarytab
//
//  Created by 黄 晨 on 12-2-13.
//  Copyright 2012年 __MyCompanyName__. All rights reserved.
//

#import "RadicalViewController.h"
#import "WordViewController.h"

@implementation RadicalViewController

@synthesize tableData, strokeData, rTableView, rSearchBar;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = NSLocalizedString(@"Radical", @"Radical");
        self.tabBarItem.image = [UIImage imageNamed:@"first"];
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.tableData = [[NSMutableArray alloc] init];
    self.strokeData = [[NSMutableArray alloc] init];
    self.rTableView.delegate = self;
    self.rTableView.dataSource = self;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    //[self.wSearchBar becomeFirstResponder];
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar {
    [searchBar setShowsCancelButton:YES animated:YES];
    self.rTableView.allowsSelection = NO;
    self.rTableView.scrollEnabled = NO;
}

- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar {
    searchBar.text=@"";
    
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
    self.rTableView.allowsSelection = YES;
    self.rTableView.scrollEnabled = YES;
}

- (void)searchBarSearchButtonClicked:(UISearchBar *)searchBar {
    // You'll probably want to do this on another thread
    // SomeService is just a dummy class representing some 
    // api that you are using to do the search
    //NSArray *results = [SomeService doSearch:searchBar.text];
    NSMutableArray *strokes = [DB fetchWordStrokeByRadical:[searchBar text]];
    NSMutableArray *words = [DB fetchWordsByRadical:[searchBar text] strokes:strokes];
    //NSLog(@"%@", [DB fetchWordList:[searchBar text]]);
    //NSLog(@"%@", words);
	
    [searchBar setShowsCancelButton:NO animated:YES];
    [searchBar resignFirstResponder];
    self.rTableView.allowsSelection = YES;
    self.rTableView.scrollEnabled = YES;
    
    [self.strokeData removeAllObjects];
    [self.strokeData addObjectsFromArray:strokes];
	
    [self.tableData removeAllObjects];
    [self.tableData addObjectsFromArray:words];
    
    [self.rTableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return [tableData count];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSDictionary *dictionary = [tableData objectAtIndex:section];
    NSArray *array = [dictionary objectForKey:@"words"];
    return [array count];
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
	return [NSString stringWithFormat:@"%@ %@", [self.strokeData objectAtIndex:section], NSLocalizedString(@"stroke", @"stroke")];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *MyIdentifier = @"SearchResult";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MyIdentifier];
	
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:MyIdentifier];
    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    NSDictionary *dictionary = [self.tableData objectAtIndex:indexPath.section];
	NSArray *array = [dictionary objectForKey:@"words"];
	Word *data = [array objectAtIndex:indexPath.row];
    cell.textLabel.text = [NSString stringWithFormat:@"%@ %@", data.word, data.pinyin];
	
    return cell;
}

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    NSDictionary *dictionary = [self.tableData objectAtIndex:indexPath.section];
	NSArray *array = [dictionary objectForKey:@"words"];
	Word *wordData = [array objectAtIndex:indexPath.row];
    UITabBarController *_root = (UITabBarController *)self.parentViewController;
    [((WordViewController *)[_root.viewControllers objectAtIndex:0]).wSearchBar setText:wordData.word];
    _root.selectedIndex = 0;
    return nil;
}

- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView {
    return [self strokeData];
}

@end
